﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VersionSwitcher.Loggers;
using System.IO;
using VersionSwitcher.Constants;
using VersionSwitcher.Exceptions;

namespace VersionSwitcher.Workers
{
    public class FileWorker
    {
        private ILogger logger;
        string mainFolderPath = String.Empty;

        public FileWorker(string versionOrPath, bool isAutoMode, ILogger logger)
        {
            this.logger = logger;
            if (isAutoMode)
            {
                string currentDirectory = GetCurrentDirectory();
                string parentDirectory = GetParentDirectory(currentDirectory);
                mainFolderPath = parentDirectory + KeyWords.RootFolderPrefix + versionOrPath;
            }
            else
            {
                mainFolderPath = versionOrPath;
            }
        }

        public string MainFolderPath
        {
            get
            {
                return mainFolderPath;
            }
        }

        public bool IsFileExists(string path)
        {
            return File.Exists(path);
        }

        private string GetParentDirectory(string currentDirectory)
        {
            string res = String.Empty;
            try
            {
                DirectoryInfo dir = Directory.GetParent(currentDirectory);
                res = dir.FullName;
            }
            catch (UnauthorizedAccessException e)
            {
                throw new BusinessException(ErrorMessages.Error + ErrorMessages.NotEnoughPermissions, e);
            }
            catch (ArgumentException e)
            {
                throw new BusinessException(ErrorMessages.Error + ErrorMessages.BadPath, e);
            }
            catch (PathTooLongException e)
            {
                throw new BusinessException(ErrorMessages.Error + ErrorMessages.PathTooLong, e);
            }
            catch (DirectoryNotFoundException e)
            {
                throw new BusinessException(ErrorMessages.Error + ErrorMessages.PathNotFound, e);
            }
            catch (IOException e)
            {
                throw new BusinessException(ErrorMessages.Error + ErrorMessages.DirectoryReadOnly, e);
            }
            return res;
        }

        public string GetCurrentDirectory()
        {
            string res = String.Empty;
            try
            {
                res = Directory.GetCurrentDirectory();
            }
            catch (UnauthorizedAccessException e)
            {
                throw new BusinessException(ErrorMessages.Error + ErrorMessages.NotEnoughPermissions, e);
            }
            return res;
        }

        public void CreateMainFolder()
        {
            CreateFolder(mainFolderPath);
        }

        private void CreateFolder(string path)
        {
            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
            }
            catch (UnauthorizedAccessException e)
            {
                throw new BusinessException(ErrorMessages.Error + ErrorMessages.NotEnoughPermissions, e);
            }
            catch (ArgumentException e)
            {
                throw new BusinessException(ErrorMessages.Error + ErrorMessages.BadPath, e);
            }
            catch (PathTooLongException e)
            {
                throw new BusinessException(ErrorMessages.Error + ErrorMessages.PathTooLong, e);
            }
            catch (DirectoryNotFoundException e)
            {
                throw new BusinessException(ErrorMessages.Error + ErrorMessages.PathNotFound, e);
            }
            catch (NotSupportedException e)
            {
                throw new BusinessException(ErrorMessages.Error + ErrorMessages.BadPath, e);
            }
            catch (IOException e)
            {
                throw new BusinessException(ErrorMessages.Error + ErrorMessages.BadPath, e);
            }
        }

        public void CreateSubFolder(string relativePath)
        {
            CreateFolder(mainFolderPath + relativePath);
        }

        public void CopyFile(string sourceFileName,	string destFileName, bool overwrite)
        {
            if (!(File.Exists(destFileName) && !overwrite))
            {
                try
                {
                    File.Copy(sourceFileName, destFileName, overwrite);
                }
                catch (UnauthorizedAccessException e)
                {
                    throw new BusinessException(ErrorMessages.Error + ErrorMessages.NotEnoughPermissions, e);
                }
                catch (ArgumentException e)
                {
                    throw new BusinessException(ErrorMessages.Error + ErrorMessages.BadPath, e);
                }
                catch (PathTooLongException e)
                {
                    throw new BusinessException(ErrorMessages.Error + ErrorMessages.PathTooLong, e);
                }
                catch (DirectoryNotFoundException e)
                {
                    throw new BusinessException(ErrorMessages.Error + ErrorMessages.PathNotFound, e);
                }
                catch (FileNotFoundException e)
                {
                    throw new BusinessException(ErrorMessages.Error + ErrorMessages.PathNotFound, e);
                }
                catch (NotSupportedException e)
                {
                    throw new BusinessException(ErrorMessages.Error + ErrorMessages.BadPath, e);
                }
                catch (IOException e)
                {
                    throw new BusinessException(ErrorMessages.Error + ErrorMessages.BadPath, e);
                }
            }
        }
    }
}
